package com.handpay.zztong.hp.login.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.PasswordTransformationMethod;
import android.util.AttributeSet;
import android.view.ActionMode;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.handpay.zztong.hp.R;

public class LoginTextItemView extends RelativeLayout {
    private Context mContext;
    private String mDefaultHintText;
    private int mLeftIconResId;
    private boolean mIsPwdType = false;
    private EditText login_text_et;
    private ImageView login_icon_iv;
    private ImageView iv_clear;
    private float defaultTextSize=16f;
    private int mTextColor;//textColor
    private int mHintTextColor;//hintColor
    private float textSize;
    protected int defaultTextColor = Color.WHITE;//default text and hint Color

    public LoginTextItemView(Context context) {
        this(context, null);
    }

    public LoginTextItemView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LoginTextItemView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;
        setCustomAttributes(attrs);
        init();
    }

    private void setCustomAttributes(AttributeSet attrs) {
        TypedArray typedArray = mContext.obtainStyledAttributes(attrs, R.styleable.LoginTextItemView);
        mDefaultHintText = typedArray.getString(R.styleable.LoginTextItemView_login_text);
        mLeftIconResId = typedArray.getResourceId(R.styleable.LoginTextItemView_login_icon, -1);
        mIsPwdType = typedArray.getBoolean(R.styleable.LoginTextItemView_login_b_password, false);
        mTextColor = typedArray.getColor(R.styleable.LoginTextItemView_text_color, defaultTextColor);
        mHintTextColor = typedArray.getColor(R.styleable.LoginTextItemView_text_hint_color, defaultTextColor);
        textSize=typedArray.getDimension(R.styleable.LoginTextItemView_text_size,defaultTextSize);
        typedArray.recycle();
    }

    private void init() {
        LayoutInflater.from(mContext).inflate(R.layout.layout_login_text_item, this);
        login_text_et = (EditText) this.findViewById(R.id.login_text_et);
        login_text_et.setHint(mDefaultHintText);
        login_text_et.setTextColor(mTextColor);
        login_text_et.setHintTextColor(mHintTextColor);
        login_text_et.setTextSize(defaultTextSize);
        if (mIsPwdType) {
            login_text_et.setTransformationMethod(PasswordTransformationMethod.getInstance());
            login_text_et.setCustomSelectionActionModeCallback(new ActionMode.Callback() {

                public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                    return false;
                }

                public void onDestroyActionMode(ActionMode mode) {
                }

                public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                    return false;
                }

                public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                    return false;
                }
            });
            login_text_et.setLongClickable(false);
            login_text_et.setTextIsSelectable(false);
        }



        login_icon_iv = (ImageView) this.findViewById(R.id.login_icon_iv);
        login_icon_iv.setImageResource(mLeftIconResId);

        iv_clear = (ImageView) this.findViewById(R.id.iv_clear);
        iv_clear.setVisibility(View.INVISIBLE);
        TextWatcher mTextWatcher = new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(login_text_et.getText().toString())) {
                    iv_clear.setVisibility(View.INVISIBLE);
                } else {

                    iv_clear.setVisibility(View.VISIBLE);
                }
            }
        };
        login_text_et.addTextChangedListener(mTextWatcher);
        if (mIsPwdType) {
            login_text_et.setOnKeyListener(new OnKeyListener() {
                @Override
                public boolean onKey(View v, int keyCode, KeyEvent event) {
                    if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_DEL) {
                        login_text_et.setText("");
                    }
                    return false;
                }
            });
        }


        View.OnClickListener clearListener = new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                login_text_et.setText("");
            }
        };
        iv_clear.setOnClickListener(clearListener);
    }

    public Editable getText() {
        return login_text_et.getText();

    }

    public void setText(CharSequence text) {
        login_text_et.setText(text);
    }

    public void setTextColor(int color) {
        login_text_et.setTextColor(color);
    }
}
